import * as API from '@/constants/api'
import { OTC_SET_MONEY, OTC_SET_COIN_LIST } from '@/constants/mutation-types'
import { otcCoinInfosStorage } from '@/utils/storage'
const coinInfo = otcCoinInfosStorage.get() || {}
const moneyList = Object.keys(coinInfo)
const defaultMoney = moneyList.length > 0 ? moneyList[0] : ''
export const state = () => ({
  money: defaultMoney,
  coinInfo
})
export const getters = {
  balance (state, _, { fund }) {
    return fund.ownedCoins[state.money]
  },
  moneyInfo (state) {
    return state.coinInfo[state.money]
  },
  getMoneyInfo: state => (money) => {
    return state.coinInfo[money]
  }
}
export const mutations = {
  [OTC_SET_COIN_LIST] (state, { coinInfo, money }) {
    if (!state.money) { state.money = money }
    state.coinInfo = coinInfo
  },
  [OTC_SET_MONEY] (state, money) {
    state.money = money
  }
}
export const actions = {
  setMoney ({ commit }, money) {
    commit(OTC_SET_MONEY, money)
  },
  async getCoinList ({ commit }) {
    const { data } = await this.$axios.$post(API.OTC_COIN_LIST)
    const coinInfo = {}
    let money = ''
    data.forEach((item, index) => {
      coinInfo[item.coin_name] = item
      if (index === 0) {
        money = item.coin_name
      }
    })
    otcCoinInfosStorage.set(coinInfo)
    commit(OTC_SET_COIN_LIST, { coinInfo, money })
    return data
  },
  async getMarketList ({ commit }, params) {
    const { data } = await this.$axios.$post(API.OTC_MARKET_LIST, params)
    return data
  },
  async getMarketDetail ({ commit }, params) {
    const { data } = await this.$axios.$post(API.OTC_MARKET_DETAIL, params)
    return data
  },
  async createOrder ({ commit }, params) {
    return await this.$axios.$post(API.OTC_ORDER, params)
  },
  async publishInfo ({ commit }, params) {
    return await this.$axios.$post(API.OTC_PUBLISH_INFO, params)
  },
  async getOrderHistory ({ commit }, params) {
    return await this.$axios.$post(API.OTC_ORDER_HISTORY, params)
  },
  async otcOrderDetail ({ commit }, params) {
    return await this.$axios.$post(API.OTC_ORDER_DETAIL, params)
  },
  async delOrder ({ commit }, params) {
    return await this.$axios.$post(API.DEL_ORDER, params)
  },
  async payOrder ({ commit }, params) {
    return await this.$axios.$post(API.PAYMENT_ORDER, params)
  },
  async makeDeal (_, params) {
    return await this.$axios.$post(API.OTC_MAKE_DEAL, params)
  },
  async otcUpdateImage ({ commit }, params) {
    return await this.$axios.$post(API.OTC_UPDATE_IMAGE, params)
  },
  async otcAppeal ({ commit }, params) {
    return await this.$axios.$post(API.OTC_APPEAL, params)
  },
  async myMarket ({ commit }, params) {
    return await this.$axios.$post(API.MY_MARKET, params)
  },
  async disableMarket ({ commit }, params) {
    return await this.$axios.$post(API.DISABLE_MARKET, params)
  },
  async enableMarket ({ commit }, params) {
    return await this.$axios.$post(API.ENABLE_MARKET, params)
  },
  async deleteMarket ({ commit }, params) {
    return await this.$axios.$post(API.DELETE_MARKET, params)
  }
}
